/****************************************************************************/
/* NAME:           Stepper.c                                             */
/* PURPOSE:        Manage all Stepper                                    */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  07/07/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "stepper_motor.h"
#include "HAL_OutputManagement_Config.h"
#include "tools.h"

#define Stepper_TASK_DELAY               (10000)
#define Stepper_NB_Stepper                   (2)

/**
  * @brief  Set out.
  * @param  None
  * @retval None
  */
#define Set_output(stepper) (OUTPUT_SetOutputValue(stepper.OutputName, TRUE))

/**
  * @brief  Reset out.
  * @param  None
  * @retval None
  */
#define Reset_output(stepper)  (OUTPUT_SetOutputValue(stepper.OutputName, FALSE))


/* List of Stepper definition */
Stepper_def_t Stepper_liste[Stepper_NB_Stepper] =
{
        {0, 2000, STEP_MOTOR_4, OUTPUT_STEPPER_1, OUTPUT_STEPPER_2, OUTPUT_STEPPER_3, OUTPUT_STEPPER_4}
};

/**
 * @brief  Main function of Stepper.
 * @param  none
 * @retval None
 */
void vTaskCTRL_Stepper()
{
    xTaskCreate( (void*)vTask_Stepper_1, (void*)"Stepper1", configMINIMAL_STACK_SIZE+128, NULL, tskIDLE_PRIORITY+5, &xHandle_Task_STEPPER_1 );

    vTaskDelete(NULL);
}

/**
 * @brief  Manage Stepper 1.
 * @param  none
 * @retval None
 */
void vTask_Stepper_1()
{
    portTickType            xLastWakeTime;
    MOTOR_4_POLE_STEP       step;




    while(1)
    {
        switch(step)
        {
            case STEP_1:
            {
                Reset_output(Stepper_liste.Out_1);
                Set_output  (Stepper_liste.Out_2);
                Set_output  (Stepper_liste.Out_3);
                Reset_output(Stepper_liste.Out_4);
                step = STEP_2;

                break;
            }
            case STEP_2:
            {
                Reset_output(Stepper_liste.Out_1);
                Set_output  (Stepper_liste.Out_2);
                Reset_output(Stepper_liste.Out_3);
                Set_output  (Stepper_liste.Out_4);
                step = STEP_3;

                break;
            }
            case STEP_3:
            {
                Set_output  (Stepper_liste.Out_1);
                Reset_output(Stepper_liste.Out_2);
                Reset_output(Stepper_liste.Out_3);
                Set_output  (Stepper_liste.Out_4);
                step = STEP_4;

                break;
            }
            case STEP_4:
            {
                Set_output  (Stepper_liste.Out_1);
                Reset_output(Stepper_liste.Out_2);
                Set_output  (Stepper_liste.Out_3);
                Reset_output(Stepper_liste.Out_4);
                step = STEP_1;

                break;
            }
            case MOTOR_STOP:
            default:
            {
                Reset_output(Stepper_liste.Out_1);
                Reset_output(Stepper_liste.Out_2);
                Reset_output(Stepper_liste.Out_3);
                Reset_output(Stepper_liste.Out_4);
            }
        }

        wait(Stepper_liste[0].Stepper_x_speed);
    }
}

/**
 * @brief  Set Stepper speed
 * @param  none
 * @retval None
 */
void Stepper_SetSpeed(uint8_t id, uint32_t speed)
{
    if (id < Stepper_NB_Stepper)
    {

    }
}

/**
 * @brief  Get Stepper mode
 * @param  none
 * @retval None
 */
bool Stepper_GetMode(uint8_t id)
{
    return (Stepper_liste[id].id);
}

/**
 * @brief  Get Stepper speed
 * @param  none
 * @retval None
 */
uint32_t Stepper_GetSpeed(uint8_t id)
{
   return (Stepper_liste[id].Stepper_x_speed);
}







